<template>
    <div style="display: flex;flex-direction: column;">



        <!-- logo标志 -->
        <div class="logo-layout">
            <a href="/" :style="{ 'marginLeft': isMobile ? '0' : '10%', 'paddingLeft': isMobile ? '15px' : '0px', }">
                <img src="@/assets/images/png/liehao.png" :style="{ 'width': '120px' }">
            </a>
        </div>
        <!-- logo标志 -->

        <div class="bottom-layout">
            <div style="color: white;text-align: center;">粤ICP备05131441号 www.liehao.com由LIE HAO LIMITED拥有和使用</div>
            <div style="color: #777777;text-align: center;margin-top: 5px;">QQ：649793687/346731415 联系电话：18923849717
                公司邮箱：liehaoltd@163.com Powered by LIEHAO
                Group Company</div>
        </div>

        <div class="sticky-right-center" v-if="showStickyValue && !isMobile">


            <div style="padding: 10px 8px;display: flex;justify-content: space-between;align-items: center;">
                <div style="color: white;font-weight: bold;">在线交流</div>
                <n-icon size="20" color="#ffffff" @click=closeStickyLayout() style="cursor: pointer;">
                    <CloseOutline />
                </n-icon>
            </div>

            <div class="sticky-content">

                <a class="sticky-qq" v-for="(item, index) in onLineChatData?.contacts" :key="index" :href="item.url">
                    <img src="@/assets/images/png/QQ.png" style="width: 16px;height: 16px;margin-right: 5px;" />
                    <span style="line-height: 16px">{{ item.text }}</span>
                </a>

                <!-- <div class="sticky-qq">
                    <img src="@/assets/images/png/QQ.png" style="width: 16px;height: 16px;margin-right: 5px;" />
                    <span style="line-height: 16px">四驱车模型批发</span>
                </div>

                <div class="sticky-qq">
                    <img src="@/assets/images/png/QQ.png" style="width: 16px;height: 16px;margin-right: 5px;" />
                    <span style="line-height: 16px">遥控车模型批发</span>
                </div> -->


                <div class="height-divider"></div>
                <div style="margin-top: 5px;font-weight: 600;">服务热线：{{ onLineChatData?.hotline }}</div>
                <div style="margin-top: 5px;font-weight: 600;">QQ：{{ onLineChatData?.qqFromText }}</div>
                <div style="margin-top: 5px;font-weight: 600;">微信二维码：</div>
                <div style="margin-bottom: 5px;">
                    <img :src="onLineChatData?.wechatQrCodeSrc" style="width: 90px;height: 75px;" />
                </div>
            </div>
        </div>

        <n-back-top class="custom-back-top" style="background: transparent;" :right="isMobile ? 10 : 100"
            :bottom="isMobile ? 180 : 100">
            <!-- <div style="
        width: 50px;
        height: 50px;
        line-height: 50px;
        text-align: center;
      "> -->



            <!-- </div> -->

            <n-button text circle style="font-size: 48px; background-color: #409eff; color: white;">
                <n-icon>
                    <ArrowUpCircle />
                </n-icon>
                <!-- <img src="@/assets/images/png/back_top.png" style="width: 100%;
        height: 100%;" /> -->
            </n-button>

        </n-back-top>


    </div>

</template>

<script setup>
import { ref, onMounted } from 'vue';
import { onLineCheck } from '@/api'
// 创建一个响应式变量来存储是否为移动设备
const isMobile = ref(false);
import { ArrowUpCircle } from '@vicons/ionicons5' // 假设使用 Ionicons 图标集中的一个图标
import { CloseOutline } from '@vicons/ionicons5'
// 定义一个函数来检查用户代理
const checkDevice = () => {
    const userAgent = navigator.userAgent || navigator.vendor || window.opera;

    // 设置isMobile的值
    isMobile.value = /android/i.test(userAgent) || /iPhone|iPad|iPod/i.test(userAgent);
};

onMounted(() => {
    checkDevice();
    getOnLineCheck();
});

const showStickyValue = ref(true)

const closeStickyLayout = () => {
    showStickyValue.value = false
}


const onLineChatData = ref(null)

const getOnLineCheck = async () => {
    const result = await onLineCheck()

    const parser = new DOMParser();
    const doc = parser.parseFromString(result.data.html, 'text/html');
    // 提取所有的 <li> 标签
    const liElements = doc.querySelectorAll('li');
    // 转换为数组并获取 outerHTML 或结构化数据

    // 输出结果
    // console.log('html:', result.data.html);

    // 或者提取结构化信息（推荐）
    const contacts = Array.from(liElements).map(li => {
        const span = li.querySelector('span');
        const link = li.querySelector('a');
        const qqMatch = link?.href.match(/uin=(\\d+)/);
        return {
            text: span?.textContent.trim(),
            qq: qqMatch ? qqMatch[1] : null,
            url: link?.href
        };
    });

    // console.log('结构化数据:', contacts);


    const bottom = doc.querySelector('.onlinebox-bottom');
    const pList = bottom?.querySelectorAll('p') || [];

    let hotline = '';
    let qqFromText = '';
    let wechatQrCodeSrc = '';

    pList.forEach(p => {
        const text = p.textContent; // 原始文本


        // 1. 提取服务热线
        if (text.includes('服务热线：')) {
            hotline = text.split("：")[1].trim();
        }

        if (text.includes('服务热线:')) {
            hotline = text.split(":")[1].trim();
        }

        // // 2. 提取 QQ:123456（注意 &nbsp; 是 \u00A0）
        if (text.includes('QQ:')) {
            qqFromText = text.split(":")[1].trim();
        }

        if (text.includes('QQ：')) {
            qqFromText = text.split("：")[1].trim();
        }

        // 匹配 img 标签作为微信二维码
        const img = p.querySelector('img');
        if (img) {
            wechatQrCodeSrc = img.src;
        }
    });

    // ========== 最终结果汇总 ==========
    onLineChatData.value = {
        contacts,           // li 中的联系人列表
        hotline,     // 服务热线
        qqFromText,         // 文本中写的 QQ
        wechatQrCodeSrc     // 微信二维码图片链接
    };

    // console.log('完整提取结果:', onLineChatData.value);

}


</script>

<style scoped lang="scss">
/* 手机端样式 */
@media (max-width: 768px) {


    .height-divider {
        background-color: #f1f5f9;
        width: 100%;
        height: 1px;
        margin-top: 10px;
        margin-bottom: 5px;
    }

    .logo-layout {
        width: 100%;
        display: flex;
        align-items: center;
        margin-top: 30px;
        background-color: white;
        height: 80px;
    }
}

@media (min-width: 769px) {

    .height-divider {
        background-color: #f1f5f9;
        width: 100%;
        height: 1px;
        margin-bottom: 5px;
        margin-top: 10px;
    }

    .logo-layout {
        width: 100%;
        display: flex;
        align-items: center;
        margin-top: 30px;
        background-color: white;
        height: 90px;
    }
}

.sticky-right-center {
    position: fixed;
    top: 40%;
    right: 0;
    transform: translateY(-50%);
    z-index: 1000;
    border-radius: 4px 0 0 4px;
    width: 130px;
    background: #3A7BBF;
    color: black;
    font-size: 13px;
    box-shadow:
        0 4px 12px rgba(0, 0, 0, 0.1),
        0 8px 24px rgba(0, 0, 0, 0.08),
        0 0 0 1px rgba(0, 0, 0, 0.05);

    /* 过渡动画（可选：悬停时增强阴影） */
    transition: box-shadow 0.3s ease, transform 0.3s ease;

    .sticky-content {
        display: flex;
        flex-direction: column;
        background: white;
        padding: 5px;
        border-radius: 0px 0 4px 4px;
        color: black;

        .sticky-qq {
            margin-top: 10px;
            color: #76838f;
            display: flex;
            align-items: center;
            // cursor: pointer;
        }
    }
}

.bottom-layout {
    padding: 15px;
    background-color: #3C3C3C;
    min-height: 72px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
</style>